import javax.swing.*;
import java.beans.*;
import java.sql.*; 
import java.lang.*;
import java.util.*;

public class FichaPrestatario extends Observable {
    JFrame ventanaFichaPrestatario;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    
    boolean editando;
    M_Prestatario prestatario;
    int codPrestatario;
    
    public FichaPrestatario(int idPrestatario){
    	prestatario=new M_Prestatario();
    	prestatario.getDatos(idPrestatario);
    	levantaVista();
    	llenaVista();
    	
    }
    
    private void levantaVista(){
    	ventanaFichaPrestatario=new JFrame();
    	     jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();

        ventanaFichaPrestatario.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        ventanaFichaPrestatario.setTitle("Sistema de administraci\u00f3n.... - Ficha del material ");
        ventanaFichaPrestatario.setResizable(false);
        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Ficha Prestatario");

        jLabel3.setText("RUT:");

        jLabel4.setText("Nombres:");

        jLabel6.setText("Tipo de prestatario:");

        jLabel7.setText("Curso asociado:");

        jTextField3.setEnabled(false);

        jTextField4.setEnabled(false);

        jComboBox1.setEnabled(false);
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel5.setText("Codigo de identificacion del prestatariol:");

        jTextField1.setEnabled(false);

        jButton1.setText("Editar Datos");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("Volver");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("Telefono:");

        jTextField2.setEnabled(false);

        jLabel8.setText("Apellidos:");

        jTextField5.setEnabled(false);

        jTextField6.setEnabled(false);

        jLabel9.setText("Direccion:");

        jTextField7.setEnabled(false);

        jLabel10.setText("-");

        jTextField8.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(ventanaFichaPrestatario.getContentPane());
        ventanaFichaPrestatario.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel8)
                            .add(jLabel6)
                            .add(jLabel3)
                            .add(jLabel5)
                            .add(jLabel4)
                            .add(jLabel7)
                            .add(jLabel2)
                            .add(jLabel9))
                        .add(26, 26, 26)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jTextField1)
                            .add(jComboBox1, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jTextField7)
                            .add(jTextField6)
                            .add(jTextField5)
                            .add(jTextField2)
                            .add(jTextField4)
                            .add(layout.createSequentialGroup()
                                .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 186, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel10)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jTextField8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(jButton3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 305, Short.MAX_VALUE)
                        .add(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(17, 17, 17)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel10)
                    .add(jTextField8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(9, 9, 9)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel9)
                    .add(jTextField7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(28, 28, 28)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton3))
                .addContainerGap())
        );
        ventanaFichaPrestatario.pack();
        ventanaFichaPrestatario.setVisible(true);
    }
    
    
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
	
	this.setChanged();
	this.notifyObservers(); 
	ventanaFichaPrestatario.dispose();
    }                                        

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
	
 		if(jButton1.getText().equals("Editar Datos")){
	 		editando = true;
	 		jTextField2.setEnabled(true);
	 		jTextField3.setEnabled(true);
	        jTextField4.setEnabled(true);
	        jTextField5.setEnabled(true);
	        jTextField6.setEnabled(true);
	        jTextField7.setEnabled(true);
	        jTextField8.setEnabled(true);
	        jComboBox1.setEnabled(true) ;
	        jButton1.setText("Guardar cambios");
		    System.out.println("editando");
		}else 
		
		if(jButton1.getText().equals("Guardar cambios")){
			String rutEscrito=jTextField3.getText()+"-"+jTextField8.getText();
			System.out.println("rutescrito=rutoficial "+rutEscrito.equals(prestatario.prt_rut));
			if(rutEscrito.equals(prestatario.prt_rut)==true || validaRut(jTextField3.getText(),jTextField8.getText())==true){
		
			editando = false;
			editarDatos();
			jTextField2.setEnabled(false); 
	        jTextField3.setEnabled(false);
	        jTextField4.setEnabled(false) ;
	        jTextField5.setEnabled(false) ;
	        jTextField6.setEnabled(false) ;
	        jTextField7.setEnabled(false) ;
	        jTextField8.setEnabled(false) ;
	        jComboBox1.setEnabled(false) ;     
	        
	        
			jButton1.setText("Editar Datos");
			}else{
				JOptionPane.showMessageDialog(ventanaFichaPrestatario,
    			"El rut ingresado no es valido o ya existe.",
    			"Aviso",
    			JOptionPane.INFORMATION_MESSAGE);
			}
		}
    }                                        
    
    private void editarDatos(){
     
      prestatario.editarDatos(prestatario.prt_cod,prestatario.codigosTiposPrestatarios[jComboBox1.getSelectedIndex()], jTextField2.getText(), jTextField3.getText()+"-"+jTextField8.getText(), jTextField4.getText(), jTextField5.getText(),jTextField6.getText(),jTextField7.getText());
  
   	
    }
    
    private Boolean validaRut(String rut, String digitoVerificador){
	int rutNumero;
	try{
	Integer.parseInt(rut);
	Integer.parseInt(digitoVerificador);
		try{
		
		if(prestatario.existeRut(rut+"-"+digitoVerificador))
		return false;
		}catch(Exception e){}
	}catch (NumberFormatException e){
	return false;
	}
	
	
		
		
	return true;	
	}
    
    private void llenaVista(){
    	jTextField1.setText(""+prestatario.prt_cod);
    	jTextField2.setText(prestatario.prt_apellidos);
    	jTextField3.setText(prestatario.prt_rut.substring(0,prestatario.prt_rut.length()-2));
    	jTextField4.setText(prestatario.prt_nombres);
    	jTextField5.setText(prestatario.prt_curso);
    	jTextField6.setText(prestatario.prt_telefono);
    	jTextField7.setText(prestatario.prt_direccion);
    	jTextField8.setText(prestatario.prt_rut.substring(prestatario.prt_rut.length()-1,prestatario.prt_rut.length()));
    	getTiposPrestatarios();
    		
    }
    
    private void getTiposPrestatarios(){
 		prestatario.getTiposPrestatarios();
 		int contador=0;
 		for(int i=0;i<prestatario.numeroTiposPrestatarios;i++){
	 		jComboBox1.insertItemAt(prestatario.tiposPrestatarios[i],i);
	 		if(prestatario.prt_tipo_cod==prestatario.codigosTiposPrestatarios[i])
			    contador=i;
 		}
 				 
 		jComboBox1.setSelectedIndex(contador);
 	}

}